package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ChargeQuery;
import cn.wolfcode.crm.domain.Insurancebill;
import cn.wolfcode.crm.query.ChargeQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IChargeQueryService;
import cn.wolfcode.crm.service.IInsurancebillService;
import cn.wolfcode.crm.util.CRMConstant;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("charge")
public class ChargeQueryController {
    @Autowired
    private IChargeQueryService chargeService;
    @Autowired
    private IInsurancebillService insurancebillService;

    @RequiresPermissions("charge:index")
    @PermissionName("见费出单主页")
    @RequestMapping("index")
    public String index() {
        return "charge/list";
    }

    @RequiresPermissions("charge:list")
    @PermissionName("见费出单主页")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ChargeQueryObject qo) {
//        PageResult<ChargeQuery> result = chargeService.query(qo);
        qo.setState(CRMConstant.Insurancebill.STATE_ISSUE);
        return insurancebillService.query(qo);
    }

    @RequiresPermissions("charge:save")
    @PermissionName("缴费方法")
    @RequestMapping("save")
    public JsonResult save(ChargeQuery chargeQuery) {
        JsonResult jsonResult=new JsonResult();
//        try{
//            chargeService.save(chargeQuery);
//        }catch(Exception e){
//            e.printStackTrace();
//            jsonResult.markErrorMsg("缴费失败");
//        }
        return jsonResult;
    }

    /*    @RequiresPermissions("charge:queryInsureInfoByClientId")
        @RequestMapping("queryInsureInfoByClientId")
        @ResponseBody
        public List<ChargeQuery> queryInsureInfoByClientId(Long clientId) {
            List list = chargeService.queryInsureInfoByClientId(clientId);
            return list;
        }*/
    @RequestMapping("queryInsureInfoByClientId")
    @ResponseBody
    public PageResult<Object> queryInsureInfoByClientId(Long clientId) {
        List<ChargeQuery> list = chargeService.queryInsureInfoByClientId(clientId);
        ArrayList<Object> rows = new ArrayList<>();
        for (ChargeQuery chargeQuery : list) {
            for (Insurancebill insurancebill : chargeQuery.getIsb()) {

                rows.add(insurancebill);
            }
        }
        PageResult<Object> pageResult = new PageResult<>();
        pageResult.setTotal(list.size());
        pageResult.setRows(rows);
        return pageResult;
    }


}
